package com.example.demoapp1;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.SslErrorHandler;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
//import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "SmartCode";

    private WebView mWebView;
    private Button mbtnSmartCode;
    private Button mbtnCapture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        mWebView = (WebView)findViewById(R.id.main_web);
        mbtnSmartCode = (Button)findViewById(R.id.btnSmartCode);
        mbtnCapture = (Button)findViewById(R.id.btnCapture);

        mWebView.getSettings().setJavaScriptEnabled(true); //启用js功能
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
                super.onReceivedSslError(view, handler, error);
            }
        });
        mWebView.addJavascriptInterface(new SmartCode(this, MainActivity.this), "SmartCode");

        mbtnSmartCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "onClick: ");
                //mWebView.loadUrl("https://static-33ee7317-0197-42e6-a6e6-53017b79f59c.bspapp.com/view/#/");
                mWebView.loadUrl("http://3.115.4.116/view/#/");
                // mWebView.loadUrl("https://3platz.t.dev.pay.fun/view/#/");
                // mWebView.loadUrl("https://10.13.16.18:4173/view");
                // mWebView.loadUrl("https://172.17.48.1:4173/smartcode");
                view.setVisibility(View.INVISIBLE);
                mWebView.setVisibility(View.VISIBLE);
            }
        });

        mbtnCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWebView.evaluateJavascript("window.SmartCode.onCapture()", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        System.out.println("SmartCode.onCapture returns：" + value); // true or null
                    }
                });
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK )
        {
            // 如果正在显示WebView，则只退出WebView
            if (mWebView.getVisibility() == View.VISIBLE) {
                mbtnSmartCode.setVisibility(View.VISIBLE);
                mWebView.setVisibility(View.INVISIBLE);
                return false;
            }

            // 创建退出对话框
            AlertDialog isExit = new AlertDialog.Builder(this).create();
            // 设置对话框标题
            isExit.setTitle("系统提示");
            // 设置对话框消息
            isExit.setMessage("确定要退出吗");
            // 添加选择按钮并注册监听
            isExit.setButton("确定", listener);
            isExit.setButton2("取消", listener);
            // 显示对话框
            isExit.show();
        }
        return false;
    }
    /**监听对话框里面的button点击事件*/
    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which)
        {
            switch (which)
            {
                case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
                    finish();
                    break;
                case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
                    break;
                default:
                    break;
            }
        }
    };
}